package sunbao.ido.xsd;


import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.List;


/**
 * @Classname XPathReader
 * @Description
 * @Date 2020/03/27 11:00
 * @Created by Administrator
 */
public class XPathReader {

    private String xml;

    private Document xmlDocument = null;

    public XPathReader(String xml) {
        this.xml = xml;
    }

    public XPathReader(Document xmlDocument) {
        this.xmlDocument = xmlDocument;
    }

    public String getXml() {
        return xml;
    }

    public Document getXmlDocument() {
        return xmlDocument;
    }

    /**
     * 从一个xml加载数据
     *
     * @param xml xml字符串
     */
    public static XPathReader createXmlPathReaderByXml(String xml) {
        XPathReader xmlReader = null;
        try {
            xmlReader = new XPathReader(xml);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return xmlReader;
    }

    /**
     * 从一个XML文件中读取数据
     *
     * @param xmlFile xml文件
     * @return
     */
    public static XPathReader createXmlPathReaderByFile(File xmlFile) {
        XPathReader xmlReader = null;
        Document xmlDocument = null;
        try {
            xmlDocument = new SAXReader().read(new FileInputStream(xmlFile));
            xmlReader = new XPathReader(xmlDocument);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return xmlReader;
    }

    /**
     * 从系统环境中查找一个xml文件，加载数据
     *
     * @param res xml文件资源
     * @return
     */
    public static XPathReader createXmlPathReaderByResource(String res) {
        XPathReader xmlReader = null;
        Document xmlDocument = null;
        URL url = ClassLoader.getSystemResource(res);

        if (null == url) {
            throw new RuntimeException(res + " not found.");
        }

        try {
            xmlDocument = new SAXReader().read(new FileInputStream(new File(url.toURI())));
            xmlReader = new XPathReader(xmlDocument);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return xmlReader;
    }

    /**
     * 返回xpath所表示的结点数
     *
     * @param xpath
     * @return
     */
    public int countNode(String xpath) {
        Document fileDocument = readDocument();
        if (!findXPath(xpath)) {
            return 0;
        }
        List es = fileDocument.selectNodes(xpath);
        if (null == es) {
            return 0;
        } else {
            return es.size();
        }
    }

    /**
     * 以xpath方式得到结果的文本
     *
     * @param xpath
     * @return 如果不存在此结点, 则返回""
     */
    public String getText(String xpath) {
        String text = "";
        Document fileDocument = readDocument();
        if (!findXPath(xpath)) {
            return "";
        }
        Node node = fileDocument.selectSingleNode(xpath);
        text = node.getText();
        return text;
    }

    /**
     * 以xpath方式得到结点某属性的值
     *
     * @param xpath
     * @param attrName
     * @return
     */
    public String getAttribute(String xpath, String attrName) {
        String attr = "";
        Document fileDocument = readDocument();
        if (!findXPath(xpath)) {
            return "";
        }
        List es = fileDocument.selectNodes(xpath);
        attr = ((Element) es.get(0)).attributeValue(attrName);
        return attr;
    }

    /**
     * 查找xpath是否存在
     *
     * @param xpath
     * @return 存在返回true, 否则返回false;
     */
    public boolean findXPath(String xpath) {
        Document fileDocument = readDocument();

        try {
            List es = fileDocument.selectNodes(xpath);
            if (0 == es.size()) {
                return false;
            } else {
                return true;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 读取配置文件
     *
     * @return 返回Document对象
     */
    private Document readDocument() {
        SAXReader reader = new SAXReader();
        try {
            if (null == xmlDocument) {
                xmlDocument = reader.read(new StringReader(xml));
            }
        } catch (Exception ex) {
        }
        return xmlDocument;
    }

    public static void main(String[] args) {
        XPathReader x = XPathReader.createXmlPathReaderByFile(new File("C:\\Users\\Administrator\\Desktop\\blaze.xml"));
        System.out.println(x.getText("/Application/@SourceCode"));
    }
}
